/**
 *  发送GET请求
 * @param {*} url 
 * @param {*} params 
 * @param {*} heardersFn 
 * @returns Promise
 */
function get(url,params,heardersFn) {
    return new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function() {
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res = JSON.parse(xhr.responseText)
                resolve(res)
            } else {
                reject(xhr.status)
            }
        }
    }
    xhr.open('get',`${url}?${params}`)
    
    if(heardersFn) heardersFn(xhr)

    xhr.send(null)
    })
}




/**
 * 发送POST请求
 * 技术栈：function + ajax + callback
 * @param {*} url 
 * @param {*} params 
 * @param {*} callback 
 * @param {*} heardersFn 
 */
function post(url,params,heardersFn) {
    return new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest
        xhr.onreadystatechange = function() {
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res = JSON.parse(xhr.responseText)
                resolve(res)
            } else {
                reject(xhr.status)
            }
        }
    }
    xhr.open('post',url)
    
    xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
    if(heardersFn) heardersFn(xhr)

    xhr.send(params)
    })
}